package db.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;

public class StringTree {
	private ArrayList<Object> list = new ArrayList<Object>();
	private StringWriter sout = new StringWriter();
	private PrintWriter out = new PrintWriter(sout); 
	public StringTree branch() {
		out.close();
		list.add(sout.toString());
		StringTree child = new StringTree();
		list.add(child);
		sout = new StringWriter();
		out = new PrintWriter(sout);
		return child;
	}
	public StringTree println() {
		out.println();
		return this;
	}
	public StringTree println(String s) {
		out.println(s);
		return this;
	}
	public StringTree printf(String format, Object... args) {
		out.printf(format, args);
		return this;
	}
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (Object o : list) {
			sb.append(o.toString());
		}
		sb.append(sout.toString());
		return sb.toString();
	}
	
	public static void main(String[] args) {
		StringTree tree = new StringTree();
		StringTree startComment = tree.branch();
		tree.printf("Hallo, did you know that %s * %s = %s ?%n", 13,13,13*13);
		StringTree comment = tree.branch();
		
		tree.printf("The summer is near\n").println();
		StringTree ch2 = tree.branch();
		comment.println("(Which is by the way, the number of representatives in the norwegian parliament.)");
		tree.println("<the end>");
		startComment.println("<start>");
		
		System.out.printf("%s%n", tree);
		
	}
}
